package risk.control.project.api;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import risk.control.project.framework.PostController;

import risk.control.project.utils.TimeUtils;

import java.util.HashMap;


@RestController
@RequestMapping(value = "/main", produces = {"application/json;charset=UTF-8"})
public class MAIN_000_000_000 extends PostController {

    private static final Logger logger = LoggerFactory.getLogger(MAIN_000_000_000.class);

    //session接口
    @PostMapping(value = "/000_000_000")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(HashMap<String, String> param) {
        String requestBody = param.get("requestBody");
        if (requestBody == null || requestBody.isEmpty()) {
            return generateFail("请求body为空！");
        }
        //解析json
        JSONObject jsonObject = (JSONObject) JSON.parse(requestBody);
        String timestamp = jsonObject.getString("timestamp");
        String appVersion = jsonObject.getString("appVersion");
        String channel = jsonObject.getString("channel");
        String osVersion = jsonObject.getString("osVersion");
        String apiLevel = jsonObject.getString("apiLevel");
        String screenWidth = jsonObject.getString("screenWidth");
        String screenHeight = jsonObject.getString("screenHeight");
        String manufacturer = jsonObject.getString("manufacturer");
        String deviceUniqueId = jsonObject.getString("deviceUniqueId");
        String signature = jsonObject.getString("signature");
        //验证时间戳（前后不超过2分钟）
        long timeDifference = TimeUtils.calculate(Long.parseLong(timestamp), System.currentTimeMillis());
        long limit = 1000 * 60 * 2;
        if (Math.abs(timeDifference) > limit) {
            //时间戳超时了
            return generateFail("timestamp超时！");
        }
        //签名按照一定的顺序->全小写再md5
        String s = timestamp.toLowerCase() //
                + screenHeight.toLowerCase() //
                + appVersion.toLowerCase()//
                + osVersion.toLowerCase() //
                + deviceUniqueId.toLowerCase()//
                + screenWidth.toLowerCase()//
                + channel.toLowerCase()//
                + manufacturer.toLowerCase()//
                + apiLevel.toLowerCase()//
                + timestamp.toLowerCase(); //
        String dynamicSignature = DigestUtils.md5Hex(s);
        if (signature == null || !signature.equals(dynamicSignature)) {
            return generateFail("无效的签名！");
        }
        //封装响应体
        HashMap<String, String> resMap = new HashMap<>();
        resMap.put("sessionId", generateSessionId());//session
        long l = System.currentTimeMillis();
        resMap.put("issuanceTime", String.valueOf(l));//颁发时间
        return generateSucceed(resMap);
    }
}
